<template>
  <a-modal
    title='添加设备'
    :width='840'
    :visible='visible'
    :confirmLoading='loading'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning='loading'>
      <a-form :form='form' v-bind='formLayout'>
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-row :gutter='12'>
          <a-col :md='12' :sm='24' v-show='model && model.id > 0'>
            <a-form-item  label='主键ID'>
              <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
            </a-form-item>
          </a-col>

          <a-col :md='12' :sm='24'>
            <a-form-item label='名称'>
              <a-input v-decorator="['alarmsName']" />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item label='设备编码'>
              <a-input v-decorator="['equipmentNo']" />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item label='初始密码'>
              <a-input v-decorator="['password']" />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item label='设备类型'>
              <a-select v-decorator="['alarmsType']" allowClear>
                <a-select-option value='光伏'>
                  光伏
                </a-select-option>
                <a-select-option value='摄像头'>
                  摄像头
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item label='设备分组'>
              <a-select v-decorator="['alarmsGroup']" allowClear>
                <a-select-option v-for='item in groupList ' :key='item.name'>{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item label='详细地址'>
              <!--          <al-cascader  :value="model.address.split('/')" level="2" data-type="all" @on-change='handleAreaChange' />-->
              <a-input v-decorator="['address']" allowClear />
            </a-form-item>
          </a-col>
        </a-row>
        <dialogmap @mapChange='mapChange' @mapMove='mapMove' />
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getDeviceGroupList } from '@/api/device'
import AlCascader from '../al-cascader.vue'
import Dialogmap from '@/views/photovoltaic/Dialogmap.vue'
//在需要使用的地方引入组件

// 表单字段
const fields = ['id', 'alarmsName', 'equipmentNo', 'password', 'alarmsGroup', 'address', 'area', 'alarmsType']

export default {
  components: {
    AlCascader,
    Dialogmap
  },
  props: {
    cityValue: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this),
      groupList: [],
      abc: this.cityValue,
      selectedValue: '北京市/市辖区'
    }
  },
  created() {
    this.getGroupList()
    console.log('model', this.model)

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    mapMove(event) {
      // console.log('========>', event)
      // console.log('========>', this.form)
      this.form.setFieldsValue({
        address: event.address
      })
      const insureAdress = event.address
      const insureAdress2 = [event.position.lng, event.position.lat]
      this.model.address = insureAdress
      this.model.area = insureAdress2
      // this.model.province = res[0]?.name
      this.model.city = event.regeocode.addressComponent.province + event.regeocode.addressComponent.city + event.regeocode.addressComponent.district
      this.$emit('areaChange', event)
    },
    mapChange(event) {
      console.log('========>', event)
      const insureAdress = event.poi.district + event.poi.address + event.poi.name
      const insureAdress2 = [event.poi.location.lng, event.poi.location.lat]
      this.model.address = insureAdress
      this.model.area = insureAdress2
      // this.model.province = res[0]?.name
      this.model.city = event.poi.district
      this.$emit('areaChange', event)
    },
    handleAreaChange(res) {
      console.log('res', res)
      const area = res.map(item => item.name).join('/')
      this.model.area = area
      this.model.province = res[0]?.name
      this.model.city = res[1]?.name
      this.$emit('areaChange', area)
      // this.model && this.form.setFieldsValue({
      //   'address': address
      // })
    },
    getGroupList() {
      var params = {
        pageNum: 1,
        pageSize: 100
      }
      getDeviceGroupList(params).then(res => {
        this.groupList = res.data.list
        console.log('groupList', this.groupList)
      })
    }
  }
}
</script>
